#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

LOG="/var/log/catalyst-install.log"
kernel=$(uname -r)

build_fglrx() {
	rmmod fglrx >/dev/null 2>&1
	/usr/bin/catalyst_build_module "${kernel}" >/dev/null 2>&1
	/usr/bin/catalyst_build_module remove &>> $LOG
	
	if [ $? -ne 0 ]; then
		stat_append "ERROR: see ${LOG} for details"
		stat_die
	fi
}

check_fglrx_old() {
	compare=$(uname -v)
	if [ -e /lib/modules/${kernel}/video/fglrx.ko ]; then
		  test=$(modinfo -F description fglrx | grep "__unv:") || stat_die
	else
		return 0
	fi
	if [ "$test" != "__unv:$compare" ]; then
		return 0
	else
		return 1
	fi
}

check_fglrx() {
	compare=$(uname -r)
	test=$(modinfo fglrx | grep "vermagic:")
	case $test in
		*$compare*) return 1 ;;
		*) return 0 ;;
	esac
}

load_fglrx() {
	stat_append ": Loading fglrx"
	modprobe fglrx
	if [ $? -ne 0 ]; then
		stat_die
	else
		stat_done
	fi
}

case "$1" in
  	start)
		stat_busy "Checking fglrx"
		check_fglrx
		
		if [ $? -eq 0 ]; then
			stat_append ": Building fglrx "
			build_fglrx
		fi
		load_fglrx
	;;
	stop)
		stat_busy "Unloading fglrx"
		rmmod fglrx
		if [ $? -ne 0 ]; then
			stat_fail
		else
			stat_done
		fi
	;;
	restart)
		$0 stop
		sleep 1
		$0 start
	;;
	status)
		if [ -n "$(lsmod | grep fglrx)" ]; then
			stat_busy "Module fglrx is loaded"
			stat_done
		else
			stat_busy "Module fglrx is not loaded"
			stat_fail
		fi
	;;
  	*)
    	echo "Usage: $0 {start|stop|restart|status}"
esac

